package com.example.itshome.network;

import android.app.Activity;
import android.widget.TextView;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.entity.CommonEntity;
import com.example.itshome.entity.MyInteger;
import com.example.itshome.manager.UserInfoManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetCommonRequest extends AbstractRecycleVIewGetRequest<CommonEntity> {

    private int circleId;
    private MyInteger commonPageIndex;
    private List<String> images;
    private Runnable initViewPager;
    private TextView commonCountTextView;
    private Runnable finishRunnable;

    public GetCommonRequest setFinishRunnable(Runnable finishRunnable) {
        this.finishRunnable = finishRunnable;
        return this;
    }

    public GetCommonRequest setCommonCountTextView(TextView commonCountTextView) {
        this.commonCountTextView = commonCountTextView;
        return this;
    }

    public GetCommonRequest setCircleId(int circleId) {
        this.circleId = circleId;
        return this;
    }

    public GetCommonRequest setCommonPageIndex(MyInteger commonPageIndex) {
        this.commonPageIndex = commonPageIndex;
        return this;
    }

    public GetCommonRequest setImages(List<String> images) {
        this.images = images;
        return this;
    }

    public GetCommonRequest setInitViewPager(Runnable initViewPager) {
        this.initViewPager = initViewPager;
        return this;
    }

    public GetCommonRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "moment/details?username="+ UserInfoManager.getInstance().getUser().getUsername()+
                "&id="+circleId+
                "&page="+commonPageIndex.value;
    }

    @Override
    protected void handleResult(String result) {
        try {
            JSONObject jsonObject=new JSONObject(result);
            int code=jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            jsonObject=jsonObject.getJSONObject("data");
            JSONArray pictureJsonArray= jsonObject.getJSONArray("pictures");
            JSONArray commentJsonArray= jsonObject.getJSONArray("comments");
            int picLen= pictureJsonArray.length();
            int commentLen=commentJsonArray.length();
            int commentCount= jsonObject.getInt("commentsCount");
            if(commonPageIndex.value==0){
                List<String>  pictures=images;
                if(picLen>0){
                    for(int i=0;i<picLen;i++){
                        pictures.add(DOMAIN_HOST+IMAGES+pictureJsonArray.getJSONObject(i).getString("storagePath"));
                    }
                }
            }
            List<CommonEntity>  comments=new ArrayList<>();
            if(commentLen>0){
                commonPageIndex.value++;
                for (int i=0;i<commentLen;i++){
                    CommonEntity entity=new CommonEntity();
                    JSONObject item=commentJsonArray.getJSONObject(i);
                    entity.setHeader(DOMAIN_HOST+IMAGES+ item.getString("userHeadPath"))
                            .setNickname(item.getString("nickname"))
                            .setMessage(item.getString("content"))
                            .setTime(item.getString("postTime"));
                    comments.add(entity);
                }
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    for (CommonEntity entity:comments){
                        addItem(entity);
                    }
                    if(initViewPager!=null)
                    initViewPager.run();
                    commonCountTextView.setText("共"+commentCount+"条评论");

                }
            });

        }catch (JSONException e){
            e.printStackTrace();
        }
    }

    @Override
    protected void afterResult() {
        if(finishRunnable!=null)
        finishRunnable.run();
    }
}
